//var basePath = "http://127.0.0.1/GraduationProject";
// var basePath = "http://123.206.33.201/gp/";
var allCartlistLength;
var accountList = [];
var goodsDetail;//商品信息

$(function(){
	getAllShopCartInfo();

	// 结算
	$("#settleAccount").on("click",function(){
		account();
	});
	
});

/**
 * 获取当前登录用户下的所有购物车信息
 */
function getAllShopCartInfo( callback){
	
	$.ajax({
		type:"get",
		url:basePath + "/cart/list",
		async:true,
		success:function(data){
			if(data){
				console.log(data.obj);
				if(data.obj.length > 0 ){
					callBack(data);
				}else{
					console.log(0);
					$(".commodityList").html("没有数据！")
				}
				$("#shopCardLink span.text").html(data.obj.length);
				
			}
		}
	});
}

/**
 * 购物车回调
 */
function callBack(data){
	var $commodityList = $(".commodityList");
	$commodityList.html("");
	
	goodsDetail = data.obj;
	allCartlistLength = data.obj.length;
	for(var i in goodsDetail){
		var images = goodsDetail[i].pic.split(",");
		var $cListInfo = $("<div class='cListInfo clearfix'></div>");
		var $inputBox = $("<div class='inputBox fl-l'><img class='ckIcon'  src='images/icon/noCheck.png' onclick='changeChecked(this)' data-stat='false' data-goodsId='"+goodsDetail[i].goodsId+"'><img class='ckedIcon' src='images/icon/checked.png' onclick='changeChecked(this)' data-stat='true' data-goodsId='-1' style='display: none;'/></div>");
		var $infoList = $("<div class='infoList fl-l clearfix'></div>");
		
		var $picAndInfo = $("<div class='picAndInfo fl-l clearfix'>" +
				"<div class='pic fl-l'><img src='"+images[0]+"'></div><div class='info fl-l'><a>"+goodsDetail[i].goodsName+"</a></div></div>");
		var $price = $("<div class='price fl-l'><p>￥<span>"+goodsDetail[i].price+"</span></p></div>");
		var $count = $("<div class='count fl-l'><div class='addCount clearfix'><span class='cut' onclick='changeNum(this)'>-</span>" +
				"<span class='num'>"+goodsDetail[i].number+"</span><span class='add' onclick='changeNum(this)'>+</span></div></div>");
		var $countPrice = $("<div class='countPrice fl-l'><p>￥<span>"+(goodsDetail[i].number*goodsDetail[i].price)+"</span></p></div>");
		var $operate = $("<div class='operate fl-l'><span onclick='removeCartInfo("+goodsDetail[i].goodsId+")'>删除</span></div>");
		$infoList.append($picAndInfo);
		$infoList.append($price);
		$infoList.append($count);
		$infoList.append($countPrice);
		$infoList.append($operate);
		$cListInfo.append($inputBox);
		$cListInfo.append($infoList);
		$commodityList.append($cListInfo);
	}
}

/**
 * 数量、价格的改变
 */
function changeNum(obj){
	var number = $(obj).siblings("span.num").html();
	var singlePrice = $(obj).parent().parent().prev().children("p").find("span").html();
	
	if($(obj).attr("class") == "cut"){
		if(number == 1){
			number = 1;
		}else{
			number --;
		}
		
	}else if($(obj).attr("class") == "add"){
		number ++;
	}
	
	$(obj).siblings("span.num").html(number);
	$(obj).parent().parent().next().children("p").find("span").html(number*singlePrice);
}

/**
 * 删除
 */
function removeCartInfo(cartid){
	
	$.ajax({
		type:"post",
		url:basePath + "/cart/del/" + cartid,
		async:true,
		success:function(data){
			if(data){
				console.log(data);
				getAllShopCartInfo();
			}
		}
	});
}

/**
 * 商品合计
 */
var countMon = 0,count = 0;
var goodsidList = [];
function changeChecked(obj){
	
	$(obj).toggle();
	$(obj).siblings("img").toggle();
	var stat = $(obj).attr("data-stat");
	console.log(stat);
	var mon = $(obj).parent().next().children("div.countPrice").find("span").html();
	
	if(stat == "false"){
		countMon += parseInt(mon);
		count ++;
		goodsidList.push($(obj).attr("data-goodsId"));
	}else if(stat == "true"){
		countMon -= parseInt(mon);
		count --;
		goodsidList.pop();
	}else if(stat == "ckIpt"){
		
	}else if(stat == "ckedIpt"){
		
	}
	accountList = [];
	
	if(count == allCartlistLength){
		$(".checkInpt").hide();
		$(".checkedInpt").show();
	}else{
		$(".checkInpt").show();
		$(".checkedInpt").hide();
	}
	
	$("#countGoodsMon").html(countMon);
	$("#shouldPay").html(countMon);
}


/**
 * 下单
 */
function account(){
	for(var i in goodsidList){
		for(var j in goodsDetail){
			if(goodsidList[i] == goodsDetail[j].goodsId){
				accountList.push(goodsDetail[j]);
			}
		}
	}
	console.log(accountList);
	
	var account = [];
	for(var i = 0;i<accountList.length;i++){
		var list = {};
		list.goodsId = accountList[i].goodsId;
		list.number = accountList[i].number;
		list.price = accountList[i].price;
		account.push(list);
	}
	console.log(account);
	
	var params= JSON.stringify(account);
 
	$.ajax({
		type:"post",
		url:basePath + "/orderGoods/account",
		data:params,
		async:true,
		dataType : 'json',
        contentType : 'application/json',
		success:function(data){
			if(data.status == "200"){
				console.log(data);
				sessionStorage.setItem("orderCode",data.obj);
				window.location.href = "payPage.html";
			
			}
		}
	});
}













